//-----------------------------------------------------------------------
// <copyright file="SignInControl.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger.UI
{
    using System;
    using System.Diagnostics;
    using System.Windows.Browser;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents a Windows Live Messenger sign-in control.
    /// </summary>
    public sealed class SignInControl
    {
        private readonly MessengerEntity entity;

        /// <summary>
        /// Initializes a new instance of the <see cref="SignInControl"/> class.
        /// </summary>
        /// <param name="control">The HTML element to host the control within.</param>
        /// <param name="privacyStatementUrl">The privacy statement URL.</param>
        /// <param name="channelUrl">The channel URL.</param>
        /// <param name="market">The market.</param>
        public SignInControl(
            HtmlElement control,
            string privacyStatementUrl,
            string channelUrl,
            string market)
            : this(GetElementId(control), privacyStatementUrl, channelUrl, market)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SignInControl"/> class.
        /// </summary>
        /// <param name="controlId">The ID of the HTML element to host the control within.</param>
        /// <param name="privacyStatementUrl">The privacy statement URL.</param>
        public SignInControl(
            string controlId,
            string privacyStatementUrl)
            : this(controlId, privacyStatementUrl, string.Empty, string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SignInControl"/> class.
        /// </summary>
        /// <param name="controlId">The ID of the HTML element to host the control within.</param>
        /// <param name="privacyStatementUrl">The privacy statement URL.</param>
        /// <param name="channelUrl">The channel URL. Optional.</param>
        public SignInControl(
            string controlId,
            string privacyStatementUrl,
            string channelUrl)
            : this(controlId, privacyStatementUrl, channelUrl, string.Empty)
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SignInControl"/> class.
        /// </summary>
        /// <param name="controlId">The ID of the HTML element to host the control within.</param>
        /// <param name="privacyStatementUrl">The privacy statement URL.</param>
        /// <param name="channelUrl">The channel URL. Optional.</param>
        /// <param name="market">The market. Optional.</param>
        public SignInControl(
            string controlId,
            string privacyStatementUrl,
            string channelUrl,
            string market)
        {
            if (String.IsNullOrEmpty(controlId))
            {
                throw ExceptionHelper.CreateArgumentNullException("controlId");
            }

            if (String.IsNullOrEmpty(privacyStatementUrl))
            {
                throw ExceptionHelper.CreateArgumentNullException("privacyStatementUrl");
            }

            try
            {
                this.entity = MessengerEntity.CreateInstance(typeof(SignInControl), controlId, privacyStatementUrl, channelUrl, market);
                this.entity.AttachEvent("AuthenticationCompleted", this.OnAuthenticationCompleted);
            }
            catch (ArgumentException e)
            {
                throw new TypeLoadException("Failure has occurred while loading a type. Please ensure the Windows Live Messenger Library JavaScript has been referenced on your page.", e);
            }
        }

        private static string GetElementId(HtmlElement control)
        {
            if (control == null)
            {
                throw ExceptionHelper.CreateArgumentNullException("control");
            }
            return control.Id;
        }

        /// <summary>
        /// Gets the privacy statement URL.
        /// </summary>
        public string PrivacyStatementUrl
        {
            get { return this.entity.GetProperty<string>("PrivacyStatementUrl"); }
        }

        /// <summary>
        /// Occurs when the User has authenticated.
        /// </summary>
        public event AuthenticationCompletedEventHandler AuthenticationCompleted;

        private void OnAuthenticationCompleted(object sender, MessengerEventArgs e)
        {
            this.OnAuthenticationCompleted(
                new AuthenticationCompletedEventArgs(
                    new MessengerIdentity(e.Entity.GetProperty("Identity"))));
        }

        /// <summary>
        /// Raises the <see cref="AuthenticationCompleted"/> event.
        /// </summary>
        /// <param name="e">
        /// An <see cref="AuthenticationCompletedEventArgs"/> object containing event data.
        /// </param>
        private void OnAuthenticationCompleted(AuthenticationCompletedEventArgs e)
        {
            if (this.AuthenticationCompleted != null)
            {
                this.AuthenticationCompleted(this, e);
            }
        }
    }
}